package com.zat.mes.andrmes.dofun.work.vm

import android.app.Application
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.BaseApplication
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.*
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import retrofit2.Call
import retrofit2.Response
import java.util.logging.Logger

/**
 * @author zy
 * @date :2020-02-17 16:53
 * @description: 审核 取消审核model
 */
class WorkViewModel : ViewModel() {

    private val authData: MutableLiveData<List<Long>> = MutableLiveData()
    private val orgClassListData: MutableLiveData<MutableList<ClassResult>> = MutableLiveData()
    private val empListData: MutableLiveData<MutableList<EmpBeanItem>> = MutableLiveData()
    private val produceData: MutableLiveData<MutableList<DayPlanProduceBeanItem>> =
        MutableLiveData()

    private val messageData: MutableLiveData<String> = MutableLiveData()

    private val programSchedulingData: MutableLiveData<MutableList<ProgramSchedulingBean>> =
        MutableLiveData()


    fun getAuthList(): MutableLiveData<List<Long>> {
        return authData
    }
    fun getMessageData(): MutableLiveData<String> {
        return messageData
    }

    fun getOrgClass(): MutableLiveData<MutableList<ClassResult>> {
        return orgClassListData
    }

    fun getEmpList(): MutableLiveData<MutableList<EmpBeanItem>> {
        return empListData
    }

    fun getProduceData(): MutableLiveData<MutableList<DayPlanProduceBeanItem>> {
        return produceData
    }

    fun getProgramSchedulingData(): MutableLiveData<MutableList<ProgramSchedulingBean>> {
        return programSchedulingData
    }

    /**
     * 获取权限
     */
    fun loadAuthList(empId: Long) {
        RetrofitManager.apiService(ApiService::class.java)
            .getAuth(empId)
            .enqueue(object : MesCallBack<BaseResponse<List<Long>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<Long>>>?,
                    response: Response<BaseResponse<List<Long>>>?
                ) {
                    authData.postValue(response?.body()?.result)
                }

            })
    }


    /**
     * 获取班组列表
     */
    fun loadClassList(ids: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getOrgClassList(ids)
            .enqueue(object : MesCallBack<BaseResponse<List<List<ClassResult>>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<List<ClassResult>>>>?,
                    response: Response<BaseResponse<List<List<ClassResult>>>>?
                ) {

                    val result = response?.body()?.result
                    if (response?.body()?.success != null) {
                        if (response?.body()?.success!!) {
                            val willBeanList: MutableList<ClassResult> = ArrayList()
                            result?.forEach { willBeanList.addAll(it) }
                            orgClassListData.postValue(willBeanList)
                        }else{
                           // Toast.makeText(BaseApplication.appContext, response?.body()?.message+"____", Toast.LENGTH_SHORT)

                            messageData.postValue(response?.body()?.message)
                        }
                    }
                }

                override fun onFailure(
                    call: Call<BaseResponse<List<List<ClassResult>>>>,
                    t: Throwable
                ) {
                    Toast.makeText(BaseApplication.appContext, "____", Toast.LENGTH_SHORT)

                }

            })
    }

    /**
     * 获取每个班组下的人员列表
     */
    fun loadClassEmpList(
        currentPage: Int,
        pageSize: Int,
        classId: Long,
        day: String,
        needPlan: Boolean
    ) {
        RetrofitManager.apiService(ApiService::class.java)
            .getClassEmpList(currentPage, pageSize, classId)
            .enqueue(object : MesCallBack<BaseResponse<EmpBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<EmpBean>>?,
                    response: Response<BaseResponse<EmpBean>>?
                ) {
                    val result = response?.body()?.result
                    if (needPlan) {
                        loadDayPlayEmpList(classId, day, result?.items, 0, Int.MAX_VALUE)
                    } else {
                        empListData.postValue(result?.items as MutableList<EmpBeanItem>?);
                    }
                }

            })
    }

    /**
     * 查询日生产计划人员
     */
    fun loadDayPlayEmpList(
        classId: Long,
        day: String,
        items: List<EmpBeanItem>?,
        currentPage: Int,
        pageSize: Int
    ) {
        RetrofitManager.apiService(ApiService::class.java)
            .getDayPlayEmplist(classId, day, currentPage, pageSize)
            .enqueue(object : MesCallBack<BaseResponse<DayPlayEmpBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<DayPlayEmpBean>>?,
                    response: Response<BaseResponse<DayPlayEmpBean>>?
                ) {
                    val result = response?.body()?.result
                    val planList = result?.items
                    if (items != null && planList != null) {
                        for (i in items?.indices) {
                            for (k in planList?.indices) {
                                if (items?.get(i).empid == planList?.get(k).workId) {
                                    items?.get(i).quantity = planList?.get(k).sengemntNumber
                                }
                            }
                        }
                    }
                    empListData.postValue(items as MutableList<EmpBeanItem>?)
                }

            })
    }

    /**
     * 查询日生产计划
     */
    fun loadDayPlanProduce(empId: Long, day: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getDayPlanProduce(empId, day)
            .enqueue(object : MesCallBack<BaseResponse<DayPlanProduceBean>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<DayPlanProduceBean>>?,
                    response: Response<BaseResponse<DayPlanProduceBean>>?
                ) {
                    val result = response?.body()?.result
                    produceData.postValue(result?.items as MutableList<DayPlanProduceBeanItem>?)
                }

            })

    }
    /**
     * 查询日生产计划
     */
    fun loadDayPlanProduceAndMaterialUsed(empId: Long, day: String) {

        var day1=day.replace("-","/")
        RetrofitManager.apiService(ApiService::class.java)
            .findProgramByEmpIdDate( day1)
            .enqueue(object : MesCallBack<BaseResponse<ArrayList<ProgramSchedulingBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<ArrayList<ProgramSchedulingBean>>>?,
                    response: Response<BaseResponse<ArrayList<ProgramSchedulingBean>>>?
                ) {
                    val result = response?.body()?.result
                    programSchedulingData.postValue( result as MutableList<ProgramSchedulingBean>?)
                }

            })

    }

}